<?php 
	if(isset($message)){
		echo $message;
	} else {
?>
<h1>Dependents</h1>
<?php echo form_open("user/edit_dependent/{$user_id}/{$dependent->id}");?>

<table>
	<?php if(isset($dependent)):?>
	<tr>
		<td colspan="2">
			<table>
				<tr>
					<td>Firstname:</td>
					<td><input type="text" name="dependent_first_name[]" value="<?php echo $dependent->first_name?>">
					<input type="hidden" name="dependent_id[]" value="<?php echo $dependent->id?>">
					</td>
				</tr>
				<tr>
					<td>Lastname:</td>
					<td><input type="text" name="dependent_last_name[]" value="<?php echo $dependent->last_name?>"></td>
				</tr>
				<tr>
					<td>Middlename:</td>
					<td><input type="text" name="dependent_middle_name[]" value="<?php echo $dependent->middle_name?>"></td>
				</tr>
				<tr>
					<td>Date of Birth:</td>
					<td>
						<input type="text" id="dependent_birth_date" name="dependent_birth_date[]" value="<?php echo $dependent->birth_date?>">
					</td>
				</tr>
				<tr>
					<td>Gender:</td>
					<td>
						<select name="dependent_gender[]" value="<?php echo $dependent->gender?>">
							<option value="Male">Male</option>
							<option value="Female">Female</option>
						</select>
					</td>
				</tr>
				
				<tr>
					<td>Type:</td>
					<td>
						<select name="dependent_type[]">
							<option value="Spouse">Spouse</option>
							<option value="Child">Child</option>
							<option value="Parent">Parent</option>
						</select>
					
					</td>
				</tr>
			</table>
		</td>
		
	</tr>
	<?php endif;?>
	<tr>
		<td colspan="2">
		<?php echo form_hidden("user_id", $user_id);?>
		<input type="submit" name="submit" value="Update"></td>
	</tr>
	
</table>

<?php echo form_close();?>

<?php } ?>

<script>

$(document).ready(function(){
	<?php if(isset($message)):?>
		setTimeout( function() {
			updateData();
		}, 2000 );
	<?php endif;?>

	<?php if(isset($dependent)):?>
		$('select[name^="dependent_gender"]').val('<?php echo $dependent->gender?>');
		$('select[name^="dependent_type"]').val('<?php echo $dependent->type?>');
	<?php endif;?>

	$("#dependent_birth_date").datepicker();
	$("#dependent_birth_date").datepicker( "option", "dateFormat", 'yy-mm-dd' );
	<?php if(isset($dependent->birth_date) && (int)($dependent->birth_date) > 0 ):?>
	$("#dependent_birth_date").datepicker( "setDate" , "<?php echo $dependent->birth_date?>" );
	<?php endif;?>
})
<?php if(isset($depId)):?>
function updateData(){
	
	first_name = "<?php echo isset($dependents)? $dependents['first_name'][0]: ''?>";
	last_name = "<?php echo isset($dependents)? $dependents['last_name'][0]: ''?>";
	middle_name = "<?php echo isset($dependents)? $dependents['middle_name'][0]: ''?>";
	dob = "<?php echo isset($dependents)? $dependents['birth_date'][0]: ''?>";
	gender = "<?php echo isset($dependents)? $dependents['gender'][0]: ''?>";
	type = "<?php echo isset($dependents)? $dependents['type'][0]: ''?>";
	id = "<?php echo $depId[0];?>";
	fullname = first_name;
	if(middle_name != '') fullname += ' ' + middle_name;
	if(last_name != '') fullname += ' ' + last_name;
	parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_full_name"]').text(fullname);
	//parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_last_name"]').text(last_name);
	//parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_middle_name"]').text(middle_name);
	parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_birth_date"]').text(dob);
	parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_gender"]').text(gender);
	parent.$('tr#tr_dependent_' + id).find('span[name^="dependent_type"]').text(type);
	//alert(first_name);
	//alert(parent.$('li#dep_' + id).find('input[name^="dependent_first_name"]').val(first_name));
	parent.highlight('tr#tr_dependent_' + id);
	parent.$.fancybox.close();
}
<?php endif;?>
</script>
